package com.hejia.alauda.service;

import java.util.Date;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.hejia.alauda.entity.Honor;
import com.hejia.alauda.entity.example.HonorExample;
import com.hejia.alauda.persistence.mybatis.HonorMapper;
import com.hejia.alauda.utils.Pager;

@Service
public class HonorServiceImpl implements HonorService {

	@Resource
	private HonorMapper honorMapper;

	@Override
	public Pager<Honor> findHonors(Pager<Honor> pager, String title) {
		HonorExample example = new HonorExample();
		if (StringUtils.isNotEmpty(title)) {
			example.appendCriterion("title like", "%" + title + "%");
		}
		example.setOrderByClause("id desc");
		pager.setList(this.honorMapper.selectByExample(example, pager));
		pager.setTotalCount(this.honorMapper.countByExample(example));

		return pager;
	}

	@Override
	public Honor addHonor(Honor honor) {
		honor.setCreateDate(new Date());
		honor.setModifyDate(new Date());
		this.honorMapper.insert(honor);
		return honor;
	}

	@Override
	public void deleteHonor(Long id) {
		this.honorMapper.deleteByPrimaryKey(honorMapper.selectByPrimaryKey(id));
	}

}
